<template>
  <a-row :gutter="10">
    <a-col :md="8" :sm="24">
      <a-card :bordered="false">
        <div style="background: #fff;padding-left:16px;height: 100%; margin-top: 5px">
          <!-- <a-input-search @search="onSearch" style="width:100%;margin-top: 10px" placeholder="请选择项目类型" /> -->
          <!-- 树-->

          <template>
            <!--项目类型-->
            <a-tree
              showLine
              :selectedKeys="selectedKeys"
              :checkStrictly="true"
              @select="onSelect"
              :dropdownStyle="{ maxHeight: '100px', overflow: 'auto' }"
              :treeData="objectTree"
              :expandedKeys="iExpandedKeys"
              defaultExpandAll
            />
          </template>
        </div>
      </a-card>
    </a-col>
    <a-col :md="16" :sm="24">
      <a-card :bordered="true">
        <a-button class="editable-add-btn" @click="handleAdd" style="margin-bottom: 8px">新增</a-button>
        <a-button class="editable-add-btn" @click="handleEdit" style="margin-bottom: 8px">编辑</a-button>
        <a-table
          bordered
          :row-selection="{ selectedRowKeys: selectedRowKeys, selectedRows: selectedRowKeys, onChange: onSelectChange }"
          :data-source="dataSource"
          :columns="columns"
        >
        </a-table>
      </a-card>
    </a-col>

    <projectinfo-modal ref="modalForm" @ok="modalFormOk"></projectinfo-modal>
  </a-row>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ProjectinfoModal from './modules/ProjectinfoModal'
import { queryprojecttype } from '@/api/erp/commonapi'
import { getAction } from '../../../../../api/manage'

export default {
  name: 'ProjectinfoList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ProjectinfoModal
  },
  data() {
    return {
      description: '项目信息管理页面',
      objectTree: [],
      treeData: [],
      selectedKeys: [],
      iExpandedKeys: [],
      selectedRowKeys:[],
      selectedRows:[],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '项目编号',
          align: 'center',
          dataIndex: 'projectcode'
        },
        {
          title: '项目任务',
          align: 'center',
          dataIndex: 'projectname'
        },
        {
          title: '是否有效',
          align: 'center',
          dataIndex: 'isvalid',
          customRender:function(text){
            return text === "true" ? "是" : "否";
          }
        },
        {
          title: '项目开始日期',
          align: 'center',
          dataIndex: 'fromdate',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '项目结束日期',
          align: 'center',
          dataIndex: 'enddate',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
      ],
      url: {
        list: '/base/projectinfo/list',
        delete: '/base/projectinfo/delete',
        deleteBatch: '/base/projectinfo/deleteBatch',
        exportXlsUrl: '/base/projectinfo/exportXls',
        importExcelUrl: 'base/projectinfo/importExcel'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    onSelectChange(selectedRowKeys,selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows=selectedRows;
    },
    handleEdit(){
      //弹出编辑窗口进行编辑
      if(this.selectedRowKeys === null || this.selectedRowKeys.length === 0){
        this.$message.warning("请选中行再进行操作!");
        return;
      }
      this.$refs.modalForm.edit(this.selectedRows[0]);
      this.$refs.modalForm.title="编辑";
    },
    onSelect (selectedKeys, e) {
      if (this.selectedKeys[0] !== selectedKeys[0]) {
        this.selectedKeys = [selectedKeys[0]];
      }
      let record = e.node.dataRef;
      console.log("record:"+JSON.stringify(record));

      localStorage.setItem("projecttypeid",this.selectedKeys[0]);

      //todo 获取当前选中行的自定义格式信息
      let searchtypeid={
        projecttypeid:this.selectedKeys[0]
      };

      getAction("/base/projectinfo/list",searchtypeid).then(res=>{
        if(res.success){
          this.dataSource=res.result.records;
        }
      })
    },
    loadData() {
      this.refresh()
    },
    refresh() {
      this.loading = true
      this.loadTree()
    },
    loadTree() {
      var that = this
      that.treeData = []
      that.objectTree = []
      console.log('loadtree')
      queryprojecttype().then(res => {
        if (res.success && res.result) {
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.treeData.push(temp)
            that.objectTree.push(temp);
            that.setThisExpandedKeys(temp)
          }
          this.loading = false
        }
      })
    },
    setThisExpandedKeys(node) {
      //只展开一级目录
      if (node.children && node.children.length > 0) {
        this.iExpandedKeys.push(node.key)
        //下方代码放开注释则默认展开所有节点

        for (let a = 0; a < node.children.length; a++) {
          this.setThisExpandedKeys(node.children[a])
        }
      }
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'projectcode', text: '项目编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'projectname', text: '项目任务', dictCode: '' })
      fieldList.push({ type: 'string', value: 'isvalid', text: '是否有效', dictCode: '' })
      fieldList.push({ type: 'date', value: 'fromdate', text: '项目开始日期' })
      fieldList.push({ type: 'date', value: 'enddate', text: '项目结束日期' })
      fieldList.push({ type: 'string', value: 'projecttypeid', text: '项目类型id', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
